from .CommonUtil import *
def serviceCount(dockerImagePrefix,dockerHub,serviceName,version):
    imageNum=execCmd("docker images|grep "+dockerHub+"/"+dockerImagePrefix+serviceName+"|grep "+version
    +"|grep -v grep |wc -l")
    return imageNum
def pushServiceImage(dockerImagePrefix,dockerHub,serviceName,version,gitPath):
    imageName=dockerImagePrefix+serviceName+":"+version
    remoteImageName=dockerHub+"/"+imageName
    execCmd("cd "+gitPath+" && docker build -t "+imageName+" .")
    execCmd("docker tag "+imageName+" "+dockerHub+"/"+imageName)
#     execCmd("docker push "+remoteImageName)
def createPublishShell(dockerHubHost,dockerImagePrefix,publish,serviceName,version,springEnv,port,javaParams):
    dockerHub=dockerHubHost+":5000"
    containerName=dockerImagePrefix+serviceName
    imageName=containerName+":"+version
    remoteImageName=dockerHub+"/"+imageName
    publishShell="#自动生成脚本"+serviceName+"\n" \
        "if [ "+dockerHubHost+" != "+publish+" ]; then #不是本机\n\t" \
                "sudo su - \n" \
        "fi\n" \
        "IMAGENUM=`docker images|grep "+dockerHub+"/"+dockerImagePrefix+serviceName+"|grep "+version+"|grep -v grep |wc -l | xargs echo` \n" \
        "if [ $IMAGENUM -eq '0' ]; then \n\t"\
                "docker pull "+remoteImageName+"\n"\
        "fi \n"\
        "docker stop "+containerName+"\n"\
        "docker rm "+containerName+"\n"\
        "if [ "+dockerHubHost+" != "+publish+" ]; then #不是本机才清理\n\t" \
                "docker images|grep "+dockerImagePrefix+serviceName+"|grep -v "+version+"|awk '{print $3}'|xargs -r docker rmi -f \n"\
        "fi\n" \
        "docker run -e TZ=\"Asia/Shanghai\" --name "+containerName+" -v /bak/logs/"+containerName+":/logs --env PARAMS=\""+javaParams+" -Dspring.profiles.active="+springEnv+" -Dserver.port="+port+" -Dxxljob.port=1"+port+"\" --network host -d "+remoteImageName+" \n"\
        "if [ "+dockerHubHost+" != "+publish+" ]; then #不是本机\n\t" \
                "exit \n"\
        "fi"
    return publishShell
def createPublishShell2(dockerHubHost,dockerImagePrefix,publish,serviceName,version,springEnv,port,javaParams,dockerParams):
    dockerHub=dockerHubHost+":5000"
    containerName=dockerImagePrefix+serviceName
    imageName=containerName+":"+version
    remoteImageName=dockerHub+"/"+imageName
    publishShell="#自动生成脚本"+serviceName+"\n" \
        "if [ "+dockerHubHost+" != "+publish+" ]; then #不是本机\n\t" \
                "sudo su - \n" \
        "fi\n" \
        "IMAGENUM=`docker images|grep "+dockerHub+"/"+dockerImagePrefix+serviceName+"|grep "+version+"|grep -v grep |wc -l | xargs echo` \n" \
        "if [ $IMAGENUM -eq '0' ]; then \n\t"\
                "docker load -i /tmp/"+dockerImagePrefix+serviceName+"-"+version+"\n"\
                "> /tmp/"+dockerImagePrefix+serviceName+"-"+version+"\n"\
        "fi \n"\
        "docker stop "+containerName+"\n"\
        "docker rm "+containerName+"\n"\
        "if [ "+dockerHubHost+" != "+publish+" ]; then #不是本机才清理\n\t" \
                "docker images|grep "+dockerImagePrefix+serviceName+"|grep -v "+version+"|awk '{if(NR>3){print $3}}'|xargs -r docker rmi -f \n"\
        "fi\n" \
        "docker run -e TZ=\"Asia/Shanghai\" --name "+containerName+" "+dockerParams+" -v /bak/logs/"+containerName+":/logs --env PARAMS=\""+javaParams+" -Dspring.profiles.active="+springEnv+" -Dserver.port="+port+" -Dxxljob.port=1"+port+"\" --network host -d "+remoteImageName+" \n"\
        "if [ "+dockerHubHost+" != "+publish+" ]; then #不是本机\n\t" \
                "exit \n"\
        "fi"
    return publishShell